#include <cstdio>
#include <algorithm>

using namespace std;

struct Node
{
    int l, w;
};

bool operator < ( Node a, Node b )
{
    if ( a.l < b.l )
        return true;
    if ( a.l > b.l )
        return false;
    if ( a.w < b.w )
        return true;
    return false;
}

int main( )
{
    Node f[ 5001 ];
    int n, i, j, t, d[ 5001 ], maxn;
    scanf("%d", &t);
    while ( t-- )
    {
        scanf("%d", &n);
        for ( i = 0; i < n; i++ )
        {
            scanf("%d%d", &f[ i ].l, &f[ i ].w);
            d[ i ] = 1;
        }
        sort( f, f + n );
        for ( i = 0; i < n; i++ )
            for ( j = 0; j < i; j++ )
                if ( f[ j ].w > f[ i ].w && d[ i ] < d[ j ] + 1 )
                    d[ i ] = d[ j ] + 1;
        maxn = 0;
        for ( i = 0; i < n; i++ )
            if ( maxn < d[ i ] )
                maxn = d[ i ];
        printf("%d\n", maxn);
    }
    return 0;
}
